package com.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.bean.User;
import com.service.UserService;

@Controller
@RequestMapping("login")
public class Login {

		@Autowired
		private UserService userService;
	
		/**
		 * AAAA
		 * @param username
		 * @param password
		 * @param model
		 * @return
		 */
		@RequestMapping("log")
	    public String log(String username,String password,Model model,HttpServletRequest request){
			User userLogin = userService.selectOne(username, password);
			
			if(userLogin!=null){
				model.addAttribute("userLogin", userLogin);
				request.getSession().setAttribute("uid", userLogin.getId());
				request.getSession().setAttribute("rid", userLogin.getRoleid());
				request.getSession().setAttribute("userlogin", userLogin);
					
				return "main/main";
	    	}else{
	    		return "redirect:/login.do";
	    	}  
	    }
		@RequestMapping("home")
		public String home(){
			return "login/home";
	    }
		
		@InitBinder
		protected void initBinder(HttpServletRequest request,
				ServletRequestDataBinder binder) throws Exception {
			binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
		}
}
